from django.db import models
from .base import TemplateModel
from django.conf import settings
from django.contrib.auth.hashers import check_password, make_password


class UserModel(TemplateModel):
    ''' 用户模型 '''
    class GenderEnum:
        """ 性别枚举类型 """
        UNKNOWN = 0     # 未知性别
        MALE = 1        # 男性
        FEMALE = 2      # 女性
    
    user_name = models.CharField(max_length=125, null=False, unique=True, verbose_name='用户名')
    nick_name = models.CharField(max_length=255, null=True, verbose_name='用户昵称')
    phone = models.CharField(null=True, max_length=20, unique=True, verbose_name='手机号')
    email = models.EmailField(null=False, unique=True, verbose_name='用户邮箱')
    GENDER_CHOICES = (
        (GenderEnum.FEMALE, '男性'),
        (GenderEnum.MALE, '女性'),
        (GenderEnum.UNKNOWN, '未知'),
    )
    gender = models.IntegerField(choices=GENDER_CHOICES, default=GenderEnum.UNKNOWN, verbose_name='性别')
    icon = models.CharField(max_length=560, null=True, verbose_name='用户头像地址')
    password = models.CharField(max_length=255, null=False, verbose_name='密码')

    def save(self, *args, **kwargs):
        """ 重写保存方法，保存过程中加密密码 """
        self.password = make_password(self.password, settings.SECRET_KEY)
        super().save(*args, **kwargs)

    def check_password(self, password) -> bool:
        """ 检查密码是否正确 """
        return check_password(password, self.password)
    